package org.lanqiao.tjut.dao;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.bean.UsersBeanTea;
import org.lanqiao.tjut.db.DBDriverFactory;

public class UserInfoManagerTeaDao {
	/**
	 * 老师 获取所有课程信息
	 * 
	 * @return 用户信息集
	 */
	public List<UsersBeanTea> getAllUsersInfo(UsersBeanTea usersBT) {
		// 查询数据库的sql语句
		String strSql = "SELECT * FROM EASCOU WHERE TEANUM=? ORDER BY COUID";
		List<UsersBeanTea> lstUsersTea = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<UsersBeanTea>(UsersBeanTea.class), new Object[] { usersBT.getTeanum() });
		return lstUsersTea;
	}

	/**
	 * 老师 获取特定课程信息
	 * 
	 * @return 用户信息集
	 */
	public UsersBeanTea getTeaCou(UsersBeanTea usersBT) {
		// 查询数据库的sql语句
		String strSql = "SELECT * FROM EASCOU WHERE TEANUM=? AND COUID=?";
		UsersBeanTea lstUsersTea = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanHandler<UsersBeanTea>(UsersBeanTea.class),
				new Object[] { usersBT.getTeanum(), usersBT.getCouid() });
		return lstUsersTea;
	}

	/**
	 * 添加老师程
	 * 
	 * @param usersB
	 *            用户信息对象
	 * @return 保存结果的记录行数
	 */

	public int addTeaCou(UsersBeanTea usersBT) {
		int re_i = 0;
		// 执行的sql语句
		String strSql = "";
		// sql语句的参数
		Object[] params = null;
		// 新增处理
		if (usersBT.getCouii() == null) {

			strSql = "INSERT INTO EASCOU( COUID,COUNAME,COUCATEGORY,COUNATYRE,COUCREDIT,TEANUM )VALUES(?,?,?,?,?,?)";
			params = new Object[] { usersBT.getCouid(), usersBT.getCouname(), usersBT.getCoucategory(),
					usersBT.getCounatyre(), usersBT.getCoucredit(), usersBT.getTeanum() };
		} else {
			strSql = "UPDATE EASCOU SET COUNAME=?,COUCATEGORY=?,COUNATYRE=?,COUCREDIT=?,TEANUM=?, COUID=? WHERE COUII=?";
			params = new Object[] { usersBT.getCouname(), usersBT.getCoucategory(), usersBT.getCounatyre(),
					usersBT.getCoucredit(), usersBT.getTeanum(), usersBT.getCouid() ,usersBT.getCouii()};
		}
		// 保存数据
		re_i = DBDriverFactory.getDBDriverInstance().update(strSql, params);
		return re_i;
	}


	/**
	 * 删除指定课程
	 * 
	 * @param userId
	 *            用户id
	 * @return 删除结果集数目
	 */
	public int deleteTeaCou(int userId) {
		int re_i = 0;
		// 执行的sql语句
		String strSql = "DELETE FROM EASCOU WHERE COUII=?";
		// 删除数据
		re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}
	/**
	 * 通过课程id获取选课学生信息
	 * 
	 * @param strUid
	 *            id主键
	 * @return 用户信息
	 */
	public List<UsersBeanTea> getUserInfoById(String strUid) {
		// 查询数据
		// 查询数据库的sql语句
		String strSql = "SELECT t.*,c.* FROM EASSTU t,EASSCO c WHERE t.USERNUM=c.USERNUM AND COUID=?";
		List<UsersBeanTea> lstUsersTea = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<UsersBeanTea>(UsersBeanTea.class), new Object[] { strUid });
		return lstUsersTea;

	}

	/**
	 * 修改学生信息
	 * 
	 * @param usersBT
	 *            用户信息对象
	 * @return 保存结果的记录行数
	 */

	public int saveUserInfo(UsersBeanTea usersBT) {
		int re_i = 0;
		// 执行的sql语句
		String strSql = "";
		// sql语句的参数
		Object[] params = null;

		// 修改处理
		strSql = "UPDATE EASSCO SET COUSCO=? WHERE USERNUM=? AND COUID=?";
		params = new Object[] { usersBT.getCousco(), usersBT.getUsernum(), usersBT.getCouid() };

		re_i = DBDriverFactory.getDBDriverInstance().update(strSql, params);
		return re_i;
	}



}
